package parser;

import antlr.RecognitionException;
import antlr.Token;

class OrExpr extends Expr {
    
    Expr left;
    Expr right;
    
    OrExpr(Expr left, Expr right) {
        this.left = left;
        this.right = right;
    }

    void delete(ByteStream out) throws RecognitionException {
        throw new RecognitionException("Cannot delete rvalue");
    }
 
    void write(ByteStream out) throws RecognitionException {
        left.write(out);
        out.write(Opcodes.COPY);
        Patch patch = out.branch(Opcodes.ON_TRUE);
        out.write(Opcodes.DROP);
        right.write(out);
//        out.write(Opcodes.END_OR);
        out.patch(patch);
    }
    
    Patch branch(boolean onTrue, ByteStream out) throws RecognitionException {
        Patch first = left.branch(true, out);
        Patch second = right.branch(onTrue, out);
        if (onTrue) {
            return new Patch.Pair(first, second);  
        } else {
             out.patch(first);
             return second;
        }
    }
    
    void writeLHS(ByteStream out) throws RecognitionException {
        throw new RecognitionException("Cannot assign to binary operator");
    }   
    
}
